package com.dsp.soundscape.server;

import java.io.File;
import java.util.ArrayList;
import java.util.logging.Logger;

import com.dsp.soundscape.model.AlbumInfo;
import com.dsp.soundscape.model.SongInfo;
import com.dsp.soundscape.util.QueryData;

public class SongScanner extends AbstractScanner {
	private static Logger log = Logger.getLogger(ThumbnailScanner.class.toString());

	@Override
	protected void fileHandler(AlbumInfo info, File[] files, File parentDir) {
		log.info("Reading Song List: " + info.getArtist() + " \"" + info.getAlbum() + "\"");
		
		for (File file : files) {
			String fnlc = file.getName().toLowerCase();
			
			if (fnlc.endsWith(".mp3") || fnlc.endsWith(".m4a") || fnlc.endsWith(".wma") || fnlc.endsWith(".flac")) {
				if (info.getSongs() == null) info.setSongs(new ArrayList <SongInfo>());
				SongInfo song = new SongInfo();
				song.setFulluri(configuration.getUriroot() + "/" + QueryData.encode(info.getFullpath(), "UTF-8") + 
						"/" + QueryData.encode(file.getName(), "UTF-8"));
				song.setFilepath(file.getName());
				
				int sidx = file.getName().indexOf(" - ");
				String songNumber = file.getName().substring(0, sidx);
				String songName = file.getName().substring(file.getName()
						.indexOf(" - ", sidx + 3) + 2, file.getName().lastIndexOf("."));
				song.setSongnumber(songNumber);
				song.setFilename(songName);
				info.getSongs().add(song);
			}
			
			if ((fnlc.endsWith(".jpg") || fnlc.endsWith(".jpeg")) && info.getImage() == null) {
				info.setImage(configuration.getUriroot() + "/" + QueryData.encode(info.getFullpath(), "UTF-8") + "/" + 
						QueryData.encode(file.getName(), "UTF-8"));
				info.setImagepath(file.getName());
			}
		}
	}
}